package com.df.springmvc.system.user.services;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.common.bean.DhtmlGridBean;
import com.common.bean.DhtmlGridFieldBean;
import com.common.util.SN_GUID;
import com.common.util.DhtmlGrid.DhtmlGridAttribuate;
import com.common.util.security.EncrypMD5;
import com.df.springmvc.supper.SupperServices;
import com.df.springmvc.system.log.services.LogServices;
import com.df.springmvc.system.user.dao.UserDao;
import com.google.gson.Gson;

/**
 * 本类只做数据运算处理
 * @author David
 */
@Service
public class UserServices extends SupperServices{
	@Autowired
	UserDao dao;
	
	@Autowired
	LogServices logServices;
	
	public List<Object> findAllEmployees(){
		// TODO Auto-generated method stub
		return null; 
	}
	public DhtmlGridBean query(DhtmlGridBean gridBean,HashMap<String,String> param_HashMap) throws Exception{
		// TODO Auto-generated method stub
		//CurrentUserBean  userBean=CurrentUserBean.loadFromSession();
		Object[] attribute=new Object[]{};
		gridBean.cleanSessionXmlFile();
		DhtmlGridAttribuate attribuate=new DhtmlGridAttribuate();
		attribuate.addFieldBean(new DhtmlGridFieldBean("","",DhtmlGridFieldBean.TYPE_CH,"40"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("guid","<center></center>",DhtmlGridFieldBean.TYPE_ED,"0"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("userAccount","用户账号",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_LEFT));
		attribuate.addFieldBean(new DhtmlGridFieldBean("userName","用户名称",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_LEFT));
		attribuate.addFieldBean(new DhtmlGridFieldBean("staffName","用户状态",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_LEFT));
		attribuate.addFieldBean(new DhtmlGridFieldBean("guid","<center></center>",DhtmlGridFieldBean.TYPE_ED,"0"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("userType","用户类型",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_LEFT));
		attribuate.addFieldBean(new DhtmlGridFieldBean("userGroupName","所属用户组",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_LEFT));
		/**
		 * 增加对应的业务代码
		 */
		ArrayList<Object> list=null;
		gridBean.setAttribuate(attribuate);
		list=dao.query(param_HashMap,attribute,0,2000);
		for(int i=0;i < list.size();i++){
			gridBean.addData(list.get(i));
			if((i + 1) % gridBean.getPageSize() == 0){
				gridBean.createSessionXmlFile();
			}
		}
		gridBean.createSessionXmlFile();
		return gridBean;
	}
	@Override
	public Object add(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		String message= (String) dao.queryVerify(paramMap);
		if(message!= null){
			return message;
		}else{
			if("0".equals(paramMap.get("staffCode"))){
				paramMap.put("staffName", "");	
				}
			
			HashMap<String,Object> map=new HashMap<String,Object>(paramMap);
			
			map.put("guid",SN_GUID.getUUID());
			map.put("status","1");
			map.put("accountStatus","1");
			map.put("createby",getCurrentUserBean().getUserId());
			map.put("createtime",new Date());
			map.put("password", EncrypMD5.encode("123456"));
			Object o = dao.add(map);
			if("0".equals(o)){
				logServices.add("用户日志","用户管理","add","添加用户信息失败");
				return "添加失败！";
			}else{
				logServices.add("用户日志","用户管理","add","添加用户信息成功");
				return "添加成功！";
			}
		}
	}
	/**
	 * 用户配置用户组
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	public Object addGroup(HashMap<String, String> paramMap) throws Exception {
		// TODO Auto-generated method stub
		String myDate = paramMap.get("tabDate");
		String userName = paramMap.get("userName");
		String userAccount = paramMap.get("userAccount");
		ArrayList<String[]> list = getList(myDate,userAccount, userName);
		Object o = dao.addGroup(paramMap,list);
		if(Integer.valueOf(o.toString())>0){
			logServices.add("用户日志","用户管理","addGroup","用户配置用户组信息成功");
			return "关联成功！";
		}else{
			logServices.add("用户日志","用户管理","addGroup","用户配置用户组信息失败");
			return "关联失败！";
		}
	}
	/**
	 * 解析Gsons数据返回为用户和用户组做关联
	 * @param myDate
	 * @param userAccount
	 * @param userName
	 * @return
	 */
	private ArrayList<String[]> getList(String myDate,String userAccount, String userName) {
		// TODO Auto-generated method stub
		ArrayList<String[]> result = new ArrayList<String[]>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Gson gson = new Gson(); 
		Map fromJson = gson.fromJson(myDate,Map.class);
		for (int i = 0; i < fromJson.size(); i++) {
			String[] ss = new String[6];
			ss[0]=SN_GUID.getUUID();//guid
			ss[1]="1";//status
			ss[2]=getCurrentUserBean().getUserId();//createBy
			ss[3]=sdf.format(new Date());//createTime
			ss[4] = userAccount;
			ArrayList<String> list = (ArrayList<String>) fromJson.get(""+i+"");
			for (int j = 0; j < list.size(); j++) {
				ss[j+5]=list.get(j);
			}
			result.add(ss);
		}
		
		return result;
	}
	@Override
	public Object upd(HashMap<String,String> map) throws Exception{
		// 编号是否重复
		String message=(String)dao.queryVerifyUpd(map);
		if(message != null){
			return message;
		}
		HashMap<String,Object> updmap=new HashMap<String,Object>(map);
		updmap.put("updateby",getCurrentUserBean().getUserId());
		updmap.put("updatetime",new Date());
		Object o=dao.upd(updmap);
		if("1".equals(o)){
			logServices.add("用户日志","用户管理","upd","修改用户信息成功");
			return "修改成功！";
		}else{
			logServices.add("用户日志","用户管理","upd","修改用户信息失败");
			throw new Exception("修改失败！");
		}
	}
	@Override
	public Object del(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		int successCount=0;
		int errorCount=0;
		for(String item:("" + paramMap.get("guid")).split(",")){
			for(String code : ("" + paramMap.get("userAccount")).split(",")){
				//Object m = delTeStaff(code);
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("guid", item);
				map.put("status", 0);
				map.put("userAccount", code);
				Object o = dao.del(map);
				if ("1".equals(o)) {
					successCount++;
				} else {
					errorCount++;
				}
			}
		}
		logServices.add("用户日志","用户管理","del","删除用户信息成功[" + successCount + "]失败[" + errorCount + "]");
		return "成功[" + successCount + "]失败[" + errorCount + "]";
	}
	@Override
	public HashMap<String,Object> view(String guid) throws Exception{
		// TODO Auto-generated method stub
		return dao.view(guid);
	}
	/**
	 * 获取人员信息
	 * @param guid 
	 * @return
	 * @throws Exception
	 */
	public Object queryStaff(String guid) throws Exception {
		// TODO Auto-generated method stub
		ArrayList<HashMap<String,Object>> list=new ArrayList<HashMap<String,Object>>();
		list=dao.getStaff(guid);
		return list;
		
		
	}
	/**
	 * 获取未被关联的用户组信息
	 * @return
	 * @throws Exception
	 */
	public Object queryUserGroup(String userAccount) throws Exception {
		// TODO Auto-generated method stub
		ArrayList<HashMap<String,Object>> list=new ArrayList<HashMap<String,Object>>();
		list=dao.getUserGroup(userAccount);
		return list;
		
		
	}
	/**
	 * 该成员已关联的用户组
	 * @param userAccount 用户账号
	 * @return
	 * @throws Exception
	 */
	public Object queryUserTeamGroup(String userAccount) throws Exception {
		// TODO Auto-generated method stub
		ArrayList<HashMap<String,Object>> list=new ArrayList<HashMap<String,Object>>();
		list=dao.getUserTeamGroup(userAccount);
		System.out.println(list.size());
		return list;
	}
	/**
	 * 重置密码
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public Object resetPassWord(HashMap<String, String> map) throws Exception {
		// TODO Auto-generated method stub
		HashMap<String,Object> updmap=new HashMap<String,Object>(map);
		updmap.put("updateby",getCurrentUserBean().getUserId());
		updmap.put("updatetime",new Date());
		updmap.put("password",EncrypMD5.encode("123456"));//md5密码加密
		Object o=dao.resetPassWord(updmap);
		if("1".equals(o)){
			logServices.add("用户日志","用户管理","resetPassWord","重置用户密码成功");
			return "重置成功！";
		}else{
			logServices.add("用户日志","用户管理","resetPassWord","重置用户密码失败");
			throw new Exception("重置失败！");
		}
	}
	
}
